////////////////////////////////////////////////////////////////////////////////


*This do-file performs the reweighting analysis found in Appendix E and produces Figures E.1-E.4 and Tables E.1-E.3.


////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////

*DiNardo-Fortin-Lemieux reweighting

////////////////////////////////////////////////////////////////////////////////

**PART ONE: UK
cd "$ukdata"

foreach i in 1992 1998 2008 2017 {
use "clean/uk_linear_reverse.dta", clear

gen dv = (year==`i')

*Step 1: estimate propensity score using logistic regression
logit dv i.temp i.parttime c.age c.age_squared c.jobtenure i.semp_boss i.semp i.union i.male ib1.marital i.uni i.immigrant i.nonwhite ib1.isco if insample [pw=relative_xw], vce(cluster year)

predict prob if insample

gen p1 = prob / (1 - prob)
sum dv if insample
local p = `r(mean)'
gen p2 = (1 - `p') / `p'
gen rw = p1 * p2
gen dfl_weight = rw * relative_xw

keep id year doim dfl_weight
bys id year doim: gen n=_N
drop if n==2
drop n

tempfile weights
save `weights'

use "clean/uk_poisson_reverse.dta", clear

merge m:1 id year doim using `weights', nogen

*Step 2: regression of insecurity on year dummies w/ reweighting
ppmlhdfe choice i.v*##ib2001.year if (insample==1) [pw=dfl_weight], a(person_year_id) vce(cluster year) d

poisson_year_mfx, save("temp/uk_nocontrols_dfl`i'.dta")

}

foreach i in 1992 1998 2008 2017 {
	use "temp/uk_nocontrols_dfl`i'.dta", clear
	gen dydx`i'=dprob1dx+dprob2dx
	gen se`i'= sqrt(variance1_nocontrols+variance2_nocontrols+variance1_2001+variance2_2001)
	gen ci_hi`i'=dydx`i'+1.96*se`i'
	gen ci_low`i'=dydx`i'-1.96*se`i'
	keep year dydx`i' se`i' ci*
	tempfile dfl`i'
	save `dfl`i''
}

*Non-reweighted regression for comparison
use "clean/uk_poisson_reverse.dta", clear

ppmlhdfe choice i.v*##ib2001.year if (insample==1) [pw=relative_xw], a(person_year_id) vce(cluster year) d

poisson_year_mfx, save("temp/poisson_marginal_effects_nocontrols.dta")

use "temp/poisson_marginal_effects_nocontrols.dta", clear
	gen dydx_nocontrols=dprob1dx+dprob2dx
	gen se_nocontrols= sqrt(variance1_nocontrols+variance2_nocontrols+variance1_2001+variance2_2001)
	gen ci_hi_nocontrols=dydx_nocontrols+1.96*se_nocontrols
	gen ci_low_nocontrols=dydx_nocontrols-1.96*se_nocontrols
	keep year dydx_nocontrols se_nocontrols ci*
tempfile nocontrols
save `nocontrols'

use `dfl1992', clear
	foreach i in 1998 2008 2017 {
		merge 1:1 year using `dfl`i'', nogen
	}
merge 1:1 year using `nocontrols', nogen

graph twoway (scatter dydx_nocontrols year, c(l) msize(small)) (scatter dydx1992 year, c(l) msize(small)), legend(label(1 "Unadjusted") label(2 "1992")) graphregion(color(white)) bgcolor(white) xtitle("Year") yline(0, lcolor(black*2)) xline(2002, lcolor(red*1.5) lpattern(dash)) xlabel(1991(2)2018, angle(45)) scale(.8) ytitle("") saving(g1, replace)

graph twoway (scatter dydx_nocontrols year, c(l) msize(small)) (scatter dydx1998 year, c(l) msize(small)), legend(label(1 "Unadjusted") label(2 "1998")) graphregion(color(white)) bgcolor(white) xtitle("Year") yline(0, lcolor(black*2)) xline(2002, lcolor(red*1.5) lpattern(dash)) xlabel(1991(2)2018, angle(45)) scale(.8) ytitle("") saving(g2, replace)

graph twoway (scatter dydx_nocontrols year, c(l) msize(small)) (scatter dydx2008 year, c(l) msize(small)), legend(label(1 "Unadjusted") label(2 "2008")) graphregion(color(white)) bgcolor(white) xtitle("Year") yline(0, lcolor(black*2)) xline(2002, lcolor(red*1.5) lpattern(dash)) xlabel(1991(2)2018, angle(45)) scale(.8) ytitle("") saving(g3, replace)

graph twoway (scatter dydx_nocontrols year, c(l) msize(small)) (scatter dydx2017 year, c(l) msize(small)), legend(label(1 "Unadjusted") label(2 "2017")) graphregion(color(white)) bgcolor(white) xtitle("Year") yline(0, lcolor(black*2)) xline(2002, lcolor(red*1.5) lpattern(dash)) xlabel(1991(2)2018, angle(45)) scale(.8) ytitle("") saving(g4, replace)

graph combine g1.gph g2.gph g3.gph g4.gph, rows(2) cols(2) graphregion(color(white)) ysize(2) xsize(2.75) title("Marginal effects of year dummies on Pr(Insecure)" "UK 1991-2018 (DFL reweighting scheme)")

*Figure E.1
graph export "$ukgraphics/uk_year_dummies_dfl_v2.png", as(png) replace

cd "$ukdata/temp"
local files: dir "`c(pwd)'" files "*"

foreach file of local files {
	cap erase `file'
}

**PART TWO, v1: US (GSS)

cd "$usdata"

foreach i in 1978 1988 1998 2008 2016 {
use "clean/gss_clean.dta", clear

gen dv = (year==`i')

*Merge in unemployment rates
merge m:1 year using "clean/us_insecure_summary.dta", keepusing(unrate)
	keep if _merge==3
	drop _merge
	
*Step 1: estimate propensity score using logistic regression
logit dv i.semp i.parttime i.onedigit_naics age age_squared i.male i.union i.immigrant i.uni i.nonwhite ib5.marital if (insample==1) [pw=wtssall]

predict prob if insample

gen p1 = prob / (1 - prob)
sum dv if insample
local p = `r(mean)'
gen p2 = (1 - `p') / `p'
gen rw = p1 * p2
gen dfl_weight = rw * wtssall

*Step 2: regression of insecurity on year dummies w/ reweighting
logit insecure ib2002.year if (insample==1) [pw=dfl_weight], vce(cluster year)
	margins, dydx(i.year) saving(temp/nocontrols_dfl`i'.dta, replace)

}

foreach i in 1978 1988 1998 2008 2016 {
	use "temp/nocontrols_dfl`i'.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb _se) (dydx`i' ci_upper`i' ci_lower`i' se`i')
	set obs 22
	replace year = 2002 in 22
	replace dydx`i' = 0 in 22
	replace ci_upper`i' = 0 in 22
	replace ci_lower`i' = 0 in 22
	replace se`i' = 0 in 22
	tempfile dfl`i'
	save `dfl`i''
}

*Non-reweighted regression for comparison
use "clean/gss_clean.dta", clear
logit insecure ib2002.year if (insample==1) [pw=wtssall], vce(cluster year)
	margins, dydx(i.year) saving(temp/nocontrols.dta, replace)

use "temp/nocontrols.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb _se) (dydx_nocontrols ci_upper_nocontrols ci_lower_nocontrols se_nocontrols)
	set obs 22
	replace year = 2002 in 22
	replace dydx_nocontrols = 0 in 22
	replace ci_upper_nocontrols = 0 in 22
	replace ci_lower_nocontrols = 0 in 22
	replace se_nocontrols = 0 in 22
tempfile nocontrols
save `nocontrols'

use `dfl1978', clear
	foreach i in 1988 1998 2008 2016 {
		merge 1:1 year using `dfl`i'', nogen
	}
merge 1:1 year using `nocontrols', nogen

graph twoway (scatter dydx_nocontrols year, c(l) msize(small)) (scatter dydx1978 year, c(l) msize(small)), legend(label(1 "Unadjusted") label(2 "1978")) graphregion(color(white)) bgcolor(white) xtitle("Year") yline(0, lcolor(black*2)) xline(2002, lcolor(red*1.5) lpattern(dash)) xlabel(1978(2)2018, angle(45)) scale(.8) ytitle("") saving(g1, replace)

graph twoway (scatter dydx_nocontrols year, c(l) msize(small)) (scatter dydx1988 year, c(l) msize(small)), legend(label(1 "Unadjusted") label(2 "1988")) graphregion(color(white)) bgcolor(white) xtitle("Year") yline(0, lcolor(black*2)) xline(2002, lcolor(red*1.5) lpattern(dash)) xlabel(1978(2)2018, angle(45)) scale(.8) ytitle("") saving(g2, replace)

graph twoway (scatter dydx_nocontrols year, c(l) msize(small)) (scatter dydx1998 year, c(l) msize(small)), legend(label(1 "Unadjusted") label(2 "1998")) graphregion(color(white)) bgcolor(white) xtitle("Year") yline(0, lcolor(black*2)) xline(2002, lcolor(red*1.5) lpattern(dash)) xlabel(1978(2)2018, angle(45)) scale(.8) ytitle("") saving(g3, replace)

graph twoway (scatter dydx_nocontrols year, c(l) msize(small)) (scatter dydx2008 year, c(l) msize(small)), legend(label(1 "Unadjusted") label(2 "2008")) graphregion(color(white)) bgcolor(white) xtitle("Year") yline(0, lcolor(black*2)) xline(2002, lcolor(red*1.5) lpattern(dash)) xlabel(1978(2)2018, angle(45)) scale(.8) ytitle("") saving(g4, replace)

graph combine g1.gph g2.gph g3.gph g4.gph, rows(2) cols(2) graphregion(color(white)) ysize(2) xsize(2.75) title("Marginal effects of year dummies on Pr(Insecure)" "US 1977-2018 (DFL reweighting scheme)")

*Figure E.2
graph export "$usgraphics/gss_year_dummies_dfl_v2.png", as(png) replace

cd "$usdata/temp"
local files: dir "`c(pwd)'" files "*"

foreach file of local files {
	cap erase `file'
}

**PART TWO, v2: US (HRS)
cd "$usdata"

foreach i in 2000 2006 2012 2018 {
use "clean/hrs_clean.dta", clear

gen dv = (year==`i')

*Merge in unemployment rates
merge m:1 year using "clean/us_insecure_summary.dta", keepusing(unrate)
	keep if _merge==3
	drop _merge

*Step 1: estimate propensity score using logistic regression
logit dv i.parttime c.age c.age_squared i.male i.union i.immigrant i.uni i.nonwhite ib5.marital if (insample==1) [pw=wtresp], vce(cluster year)

predict prob if insample

gen p1 = prob / (1 - prob)
sum dv if insample
local p = `r(mean)'
gen p2 = (1 - `p') / `p'
gen rw = p1 * p2
gen dfl_weight = rw * wtresp

*Step 2: regression of insecurity on year dummies w/ reweighting
reg insecurity ib2002.year if (insample==1) [pw=dfl_weight], vce(cluster year)
	margins, dydx(i.year) saving(temp/nocontrols_hrs_dfl`i'.dta, replace)
}

foreach i in 2000 2006 2012 2018 {
	use "temp/nocontrols_hrs_dfl`i'.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb _se) (dydx`i' ci_upper`i' ci_lower`i' se`i')
	set obs 12
	replace year = 2002 in 12
	replace dydx`i' = 0 in 12
	replace ci_upper`i' = 0 in 12
	replace ci_lower`i' = 0 in 12
	replace se`i' = 0 in 12
	tempfile dfl`i'
	save `dfl`i''
}

*Non-reweighted regression for comparison
use "clean/hrs_clean.dta", clear
reg insecurity ib2002.year if (insample==1) [pw=wtresp], vce(cluster year)
	margins, dydx(i.year) saving(temp/nocontrols_hrs.dta, replace)

use "temp/nocontrols_hrs.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb _se) (dydx_nocontrols ci_upper_nocontrols ci_lower_nocontrols se_nocontrols)
	set obs 12
	replace year = 2002 in 12
	replace dydx_nocontrols = 0 in 12
	replace ci_upper_nocontrols = 0 in 12
	replace ci_lower_nocontrols = 0 in 12
	replace se_nocontrols = 0 in 12
tempfile nocontrols
save `nocontrols'

use `dfl2000', clear
	foreach i in 2006 2012 2018 {
		merge 1:1 year using `dfl`i'', nogen
	}
merge 1:1 year using `nocontrols', nogen

graph twoway (scatter dydx_nocontrols year, c(l) msize(small)) (scatter dydx2000 year, c(l) msize(small)), legend(label(1 "Unadjusted") label(2 "2000")) graphregion(color(white)) bgcolor(white) xtitle("Year") yline(0, lcolor(black*2)) xline(2002, lcolor(red*1.5) lpattern(dash)) xlabel(1994(2)2018, angle(45)) scale(.8) ytitle("") saving(g1, replace)

graph twoway (scatter dydx_nocontrols year, c(l) msize(small)) (scatter dydx2006 year, c(l) msize(small)), legend(label(1 "Unadjusted") label(2 "2006")) graphregion(color(white)) bgcolor(white) xtitle("Year") yline(0, lcolor(black*2)) xline(2002, lcolor(red*1.5) lpattern(dash)) xlabel(1994(2)2018, angle(45)) scale(.8) ytitle("") saving(g2, replace)

graph twoway (scatter dydx_nocontrols year, c(l) msize(small)) (scatter dydx2012 year, c(l) msize(small)), legend(label(1 "Unadjusted") label(2 "2012")) graphregion(color(white)) bgcolor(white) xtitle("Year") yline(0, lcolor(black*2)) xline(2002, lcolor(red*1.5) lpattern(dash)) xlabel(1994(2)2018, angle(45)) scale(.8) ytitle("") saving(g3, replace)

graph twoway (scatter dydx_nocontrols year, c(l) msize(small)) (scatter dydx2018 year, c(l) msize(small)), legend(label(1 "Unadjusted") label(2 "2018")) graphregion(color(white)) bgcolor(white) xtitle("Year") yline(0, lcolor(black*2)) xline(2002, lcolor(red*1.5) lpattern(dash)) xlabel(1994(2)2018, angle(45)) scale(.8) ytitle("") saving(g4, replace)

graph combine g1.gph g2.gph g3.gph g4.gph, rows(2) cols(2) graphregion(color(white)) ysize(2) xsize(2.75) title("Marginal effects of year dummies on Pr(Insecure)" "US 1994-2018 (DFL reweighting scheme)")

*Figure E.3
graph export "$usgraphics/hrs_year_dummies_dfl_v2.png", as(png) replace

cd "$usdata/temp"
local files: dir "`c(pwd)'" files "*"

foreach file of local files {
	cap erase `file'
}

**PART THREE: Germany
cd "$germanydata"

foreach i in 1988 1998 2008 2018 {
use "clean/soep_clean.dta", clear

gen dv = (year==`i')

logit dv i.temp i.parttime i.semp i.immigrant c.jobtenure ib3.marital ib1.isco i.male age age_squared i.marginal i.uni if insample==1 [pw=xw], vce(cluster year)

predict prob if insample

gen p1 = prob / (1 - prob)
sum dv if insample
local p = `r(mean)'
gen p2 = (1 - `p') / `p'
gen rw = p1 * p2
gen dfl_weight = rw * xw

*Step 2: regression of insecurity on year dummies w/ reweighting
logit insecure ib2001.year if insample==1 [pw=dfl_weight], vce(cluster year)
	margins, dydx(i.year) saving(temp/soep_nocontrols_dfl`i'.dta, replace)
	
}

foreach i in 1988 1998 2008 2018 {
	use "temp/soep_nocontrols_dfl`i'.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb _se) (dydx`i' ci_upper`i' ci_lower`i' se`i')
	set obs 34
	replace year = 2001 in 34
	replace dydx`i' = 0 in 34
	replace ci_upper`i' = 0 in 34
	replace ci_lower`i' = 0 in 34
	replace se`i' = 0 in 34
	tempfile dfl`i'
	save `dfl`i''
}

*Non-reweighted regression for comparison
use "clean/soep_clean.dta", clear
logit insecure ib2001.year if insample==1 [pw=xw], vce(cluster year)
	margins, dydx(i.year) saving(temp/soep_nocontrols.dta, replace)

use "temp/soep_nocontrols.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb _se) (dydx_nocontrols ci_upper_nocontrols ci_lower_nocontrols se_nocontrols)
	set obs 34
	replace year = 2001 in 34
	replace dydx_nocontrols = 0 in 34
	replace ci_upper_nocontrols = 0 in 34
	replace ci_lower_nocontrols = 0 in 34
	replace se_nocontrols = 0 in 34
tempfile nocontrols
save `nocontrols'

use `dfl1988', clear
	foreach i in 1998 2008 2018 {
		merge 1:1 year using `dfl`i'', nogen
	}
merge 1:1 year using `nocontrols', nogen

graph twoway (scatter dydx_nocontrols year, c(l) msize(small)) (scatter dydx1988 year, c(l) msize(small)), legend(label(1 "Unadjusted") label(2 "1988")) graphregion(color(white)) bgcolor(white) xtitle("Year") yline(0, lcolor(black*2)) xline(2001, lcolor(red*1.5) lpattern(dash)) xlabel(1994(2)2018, angle(45)) scale(.8) ytitle("") saving(g1, replace)

graph twoway (scatter dydx_nocontrols year, c(l) msize(small)) (scatter dydx1998 year, c(l) msize(small)), legend(label(1 "Unadjusted") label(2 "1998")) graphregion(color(white)) bgcolor(white) xtitle("Year") yline(0, lcolor(black*2)) xline(2001, lcolor(red*1.5) lpattern(dash)) xlabel(1994(2)2018, angle(45)) scale(.8) ytitle("") saving(g2, replace)

graph twoway (scatter dydx_nocontrols year, c(l) msize(small)) (scatter dydx2008 year, c(l) msize(small)), legend(label(1 "Unadjusted") label(2 "2008")) graphregion(color(white)) bgcolor(white) xtitle("Year") yline(0, lcolor(black*2)) xline(2001, lcolor(red*1.5) lpattern(dash)) xlabel(1994(2)2018, angle(45)) scale(.8) ytitle("") saving(g3, replace)

graph twoway (scatter dydx_nocontrols year, c(l) msize(small)) (scatter dydx2018 year, c(l) msize(small)), legend(label(1 "Unadjusted") label(2 "2018")) graphregion(color(white)) bgcolor(white) xtitle("Year") yline(0, lcolor(black*2)) xline(2001, lcolor(red*1.5) lpattern(dash)) xlabel(1994(2)2018, angle(45)) scale(.8) ytitle("") saving(g4, replace)

graph combine g1.gph g2.gph g3.gph g4.gph, rows(2) cols(2) graphregion(color(white)) ysize(2) xsize(2.75) title("Marginal effects of year dummies on Pr(Insecure)" "Germany 1985-2018 (DFL reweighting scheme)")

*Figure E.4
graph export "$germanygraphics/soep_year_dummies_dfl_v2.png", as(png) replace

cd "$germanydata/temp"
local files: dir "`c(pwd)'" files "*"

foreach file of local files {
	cap erase `file'
}

////////////////////////////////////////////////////////////////////////////////

*Oaxaca-Blinder decompositions

////////////////////////////////////////////////////////////////////////////////

**PART ONE v1: US (GSS)
cd "$usdata"

*1978-1994
use "clean/gss_clean.dta", clear
gen groupvar=(year==1994)
tab onedigit_naics, gen(industry)
gen married=(marital==1)
keep if (insample==1&inlist(year,1978,1994))

mvdcmp groupvar, norm(industry1-industry9): logit insecure semp parttime age age_squared married industry* male union immigrant uni nonwhite [pw=wtssall]
mat def gss1=e(b)
mat def gss1_se = e(V)
mat def gss1_se = vecdiag(gss1_se)
local dim = `= colsof(gss1_se)'
mat def gss1_p = J(1,`dim',.)
forval i = 1/`dim' {
	mat def gss1_se[1,`i'] = sqrt(gss1_se[1,`i'])
	mat def gss1_p[1,`i'] = (1-normal(abs(gss1[1,`i'])/gss1_se[1,`i']))*2
}

mat def help = [gss1 \ gss1_p]
local dim = `= colsof(help)'
mat def gss1_p = help[2,1..`dim']

*Create empty estimates
reg insecure i.year
estimates store gss1
estadd matrix ob_b = gss1
estadd matrix ob_se = gss1_se
estadd matrix ob_p = gss1_p
estimates store gss1_table

*1994-2016
use "clean/gss_clean.dta", clear
gen groupvar=(year==2016)
tab onedigit_naics, gen(industry)
gen married=(marital==1)

keep if (insample==1&inlist(year,1994,2016))

//Industry2 variable is collinear with semp in this year pair so we need to drop it
drop if industry2
drop industry2

mvdcmp groupvar, norm(industry1-industry9): logit insecure semp parttime age age_squared married industry* male union immigrant uni nonwhite [pw=wtssall]
mat def gss2=e(b)
mat def gss2_se = e(V)
mat def gss2_se = vecdiag(gss2_se)
local dim = `= colsof(gss2_se)'
mat def gss2_p = J(1,`dim',.)
forval i = 1/`dim' {
	mat def gss2_se[1,`i'] = sqrt(gss2_se[1,`i'])
	mat def gss2_p[1,`i'] = (1-normal(abs(gss2[1,`i'])/gss2_se[1,`i']))*2
}

mat def help = [gss2 \ gss2_p]
local dim = `= colsof(help)'
mat def gss2_p = help[2,1..`dim']

*Create empty estimates
reg insecure i.year
estimates store gss2
estadd matrix ob_b = gss2
estadd matrix ob_se = gss2_se
estadd matrix ob_p = gss2_p
estimates store gss2_table

*1978-2016
use "clean/gss_clean.dta", clear
gen groupvar=(year==2016)
tab onedigit_naics, gen(industry)
gen married=(marital==1)

keep if (insample==1&inlist(year,1978,2016))

//Industry2 variable is collinear with semp in this year pair so we need to drop it
drop if industry2
drop industry2

mvdcmp groupvar, norm(industry1-industry9): logit insecure semp parttime age age_squared married industry* male union immigrant uni nonwhite [pw=wtssall]
mat def gss3=e(b)
mat def gss3_se = e(V)
mat def gss3_se = vecdiag(gss3_se)
local dim = `= colsof(gss3_se)'
mat def gss3_p = J(1,`dim',.)
forval i = 1/`dim' {
	mat def gss3_se[1,`i'] = sqrt(gss3_se[1,`i'])
	mat def gss3_p[1,`i'] = (1-normal(abs(gss3[1,`i'])/gss3_se[1,`i']))*2
}

mat def help = [gss3 \ gss3_p]
local dim = `= colsof(help)'
mat def gss3_p = help[2,1..`dim']

*Create empty estimates
reg insecure i.year
estimates store gss3
estadd matrix ob_b = gss3
estadd matrix ob_se = gss3_se
estadd matrix ob_p = gss3_p
estimates store gss3_table

*Table E.1
esttab gss1_table gss2_table gss3_table using "$tables/ob_gss_new.tex", refcat(semp "\textit{Individual covariates:}", nolabel) replace eqlabels(none) cells(ob_b(fmt(%9.4f) star pvalue(ob_p)) ob_se(fmt(%9.4f) par(( )))) keep(Summary:E Summary:C Summary:R E:semp E:parttime E:union E:uni E:male E:married E:nonwhite E:immigrant E:age E:age_squared) booktabs noobs width(\hsize) gaps coeflabel(Summary:E "Endowments" Summary:C "Coefficients" Summary:R "Raw difference" E:semp "Self-employed" E:parttime "Part-time worker" E:age "Age" E:age_squared "(Age/10)^{2}" E:male "Male" E:married "Married" E:union "Union member" E:immigrant "Immigrant" E:uni "Tertiary degree" E:nonwhite "Non-white") mlabels("1978-1994" "1994-2016" "1978-2016") nonumbers collabels("")  postfoot("\tabnotes{4}{This table displays the contribution of composition and coefficient differences to the overall difference in aggregate job insecurity between selected years, derived from the nonlinear Oaxaca-Blinder decomposition found in Powers et al. (2011). Standard errors in parentheses. *\textit{p} < 0.05, **\textit{p} < 0.01, ***\textit{p} < 0.001}") title("Oaxaca-Blinder decomposition results: US (GSS)") order(Summary:R Summary:C Summary:E E:semp E:parttime E:union E:uni E:male E:married E:nonwhite E:immigrant E:age E:age_squared)

**PART TWO, v2: US (HRS)
cd "$usdata"

*1994-2010
use "clean/hrs_clean.dta", clear
gen married=(marital==1)
tab industry, gen(ind)
drop industry
gen groupvar=(year==2010)

keep if insample&inlist(year,1994,2010)

mvdcmp groupvar, norm(ind1-ind9): reg insecurity parttime age age_squared male union immigrant uni nonwhite married ind* [pw=wtresp]

mat def hrs1=e(b)
mat def hrs1_se = e(V)
mat def hrs1_se = vecdiag(hrs1_se)
local dim = `= colsof(hrs1_se)'
mat def hrs1_p = J(1,`dim',.)
forval i = 1/`dim' {
	mat def hrs1_se[1,`i'] = sqrt(hrs1_se[1,`i'])
	mat def hrs1_p[1,`i'] = (1-normal(abs(hrs1[1,`i'])/hrs1_se[1,`i']))*2
}

mat def help = [hrs1 \ hrs1_p]
local dim = `= colsof(help)'
mat def hrs1_p = help[2,1..`dim']

*Create empty estimates
reg insecurity i.year
estimates store hrs1
estadd matrix ob_b = hrs1
estadd matrix ob_se = hrs1_se
estadd matrix ob_p = hrs1_p
estimates store hrs1_table

*2010-2016
use "clean/hrs_clean.dta", clear
gen married=(marital==1)
tab industry, gen(ind)
drop industry
gen groupvar=(year==2016)

keep if insample&inlist(year,2010,2016)

mvdcmp groupvar, norm(ind1-ind9): reg insecurity parttime age age_squared male union immigrant uni nonwhite married ind* [pw=wtresp]

mat def hrs2=e(b)
mat def hrs2_se = e(V)
mat def hrs2_se = vecdiag(hrs2_se)
local dim = `= colsof(hrs2_se)'
mat def hrs2_p = J(1,`dim',.)
forval i = 1/`dim' {
	mat def hrs2_se[1,`i'] = sqrt(hrs2_se[1,`i'])
	mat def hrs2_p[1,`i'] = (1-normal(abs(hrs2[1,`i'])/hrs2_se[1,`i']))*2
}

mat def help = [hrs2 \ hrs2_p]
local dim = `= colsof(help)'
mat def hrs2_p = help[2,1..`dim']

*Create empty estimates
reg insecurity i.year
estimates store hrs2
estadd matrix ob_b = hrs2
estadd matrix ob_se = hrs2_se
estadd matrix ob_p = hrs2_p
estimates store hrs2_table

*2000-2016
use "clean/hrs_clean.dta", clear
gen married=(marital==1)
tab industry, gen(ind)
drop industry
gen groupvar=(year==2016)

keep if insample&inlist(year,2000,2016)

mvdcmp groupvar, norm(ind1-ind9): reg insecurity parttime age age_squared male union immigrant uni nonwhite married ind* [pw=wtresp]

mat def hrs3=e(b)
mat def hrs3_se = e(V)
mat def hrs3_se = vecdiag(hrs3_se)
local dim = `= colsof(hrs3_se)'
mat def hrs3_p = J(1,`dim',.)
forval i = 1/`dim' {
	mat def hrs3_se[1,`i'] = sqrt(hrs3_se[1,`i'])
	mat def hrs3_p[1,`i'] = (1-normal(abs(hrs3[1,`i'])/hrs3_se[1,`i']))*2
}

mat def help = [hrs3 \ hrs3_p]
local dim = `= colsof(help)'
mat def hrs3_p = help[2,1..`dim']

*Create empty estimates
reg insecurity i.year
estimates store hrs3
estadd matrix ob_b = hrs3
estadd matrix ob_se = hrs3_se
estadd matrix ob_p = hrs3_p
estimates store hrs3_table

*Table E.2
esttab hrs1_table hrs2_table hrs3_table using "$tables/ob_hrs_new.tex", refcat(parttime "\textit{Individual covariates:}", nolabel) replace eqlabels(none) cells(ob_b(fmt(%9.4f) star pvalue(ob_p)) ob_se(fmt(%9.4f) par(( )))) keep(Summary:E Summary:C Summary:R E:parttime E:union E:uni E:male E:married E:nonwhite E:immigrant E:age E:age_squared) booktabs noobs width(\hsize) gaps coeflabel(Summary:E "Endowments" Summary:C "Coefficients" Summary:R "Raw difference" E:parttime "Part-time worker" E:age "Age" E:age_squared "(Age/10)^{2}" E:male "Male" E:married "Married" E:union "Union member" E:immigrant "Immigrant" E:uni "Tertiary degree" E:nonwhite "Non-white") mlabels("1994-2010" "2010-2016" "2000-2016") nonumbers collabels("")  postfoot("\tabnotes{4}{This table displays the contribution of composition and coefficient differences to the overall difference in aggregate job insecurity between selected years, derived from the nonlinear Oaxaca-Blinder decomposition found in Powers et al. (2011). Standard errors in parentheses. *\textit{p} < 0.05, **\textit{p} < 0.01, ***\textit{p} < 0.001}") title("Oaxaca-Blinder decomposition results: US (HRS)") order(Summary:R Summary:C Summary:E E:parttime E:union E:uni E:male E:married E:nonwhite E:immigrant E:age E:age_squared)

**PART TWO: Germany
cd "$germanydata"

*1986-2004
use "clean/soep_clean.dta", clear
gen groupvar=(year==2004)
tab isco, gen(industry)
gen married=(marital==1)

keep if insample&inlist(year,1986,2004)

mvdcmp groupvar, norm(industry1-industry9): logit insecure temp parttime semp immigrant jobtenure married industry* male age age_squared marginal uni [pw=xw]
mat def soep1=e(b)
mat def soep1_se = e(V)
mat def soep1_se = vecdiag(soep1_se)
local dim = `= colsof(soep1_se)'
mat def soep1_p = J(1,`dim',.)
forval i = 1/`dim' {
	mat def soep1_se[1,`i'] = sqrt(soep1_se[1,`i'])
	mat def soep1_p[1,`i'] = (1-normal(abs(soep1[1,`i'])/soep1_se[1,`i']))*2
}

mat def help = [soep1 \ soep1_p]
local dim = `= colsof(help)'
mat def soep1_p = help[2,1..`dim']

*Create empty estimates
reg insecure i.year
estimates store soep1
estadd matrix ob_b = soep1
estadd matrix ob_se = soep1_se
estadd matrix ob_p = soep1_p
estimates store soep1_table

*2004-2018
use "clean/soep_clean.dta", clear
gen groupvar=(year==2018)
tab isco, gen(industry)
gen married=(marital==1)

keep if insample&inlist(year,2004,2018)

mvdcmp groupvar, norm(industry1-industry9): logit insecure temp parttime semp immigrant jobtenure married industry* male age age_squared marginal uni [pw=xw]
mat def soep2=e(b)
mat def soep2_se = e(V)
mat def soep2_se = vecdiag(soep2_se)
local dim = `= colsof(soep2_se)'
mat def soep2_p = J(1,`dim',.)
forval i = 1/`dim' {
	mat def soep2_se[1,`i'] = sqrt(soep2_se[1,`i'])
	mat def soep2_p[1,`i'] = (1-normal(abs(soep2[1,`i'])/soep2_se[1,`i']))*2
}

mat def help = [soep2 \ soep2_p]
local dim = `= colsof(help)'
mat def soep2_p = help[2,1..`dim']

*Create empty estimates
reg insecure i.year
estimates store soep2
estadd matrix ob_b = soep2
estadd matrix ob_se = soep2_se
estadd matrix ob_p = soep2_p
estimates store soep2_table

*1986-2018
use "clean/soep_clean.dta", clear
gen groupvar=(year==2018)
tab isco, gen(industry)
gen married=(marital==1)

keep if insample&inlist(year,1986,2018)

mvdcmp groupvar, norm(industry1-industry9): logit insecure temp parttime semp immigrant jobtenure married industry* male age age_squared marginal uni [pw=xw]
mat def soep3=e(b)
mat def soep3_se = e(V)
mat def soep3_se = vecdiag(soep3_se)
local dim = `= colsof(soep3_se)'
mat def soep3_p = J(1,`dim',.)
forval i = 1/`dim' {
	mat def soep3_se[1,`i'] = sqrt(soep3_se[1,`i'])
	mat def soep3_p[1,`i'] = (1-normal(abs(soep3[1,`i'])/soep3_se[1,`i']))*2
}

mat def help = [soep3 \ soep3_p]
local dim = `= colsof(help)'
mat def soep3_p = help[2,1..`dim']

*Create empty estimates
reg insecure i.year
estimates store soep3
estadd matrix ob_b = soep3
estadd matrix ob_se = soep3_se
estadd matrix ob_p = soep3_p
estimates store soep3_table

*Table E.3
esttab soep1_table soep2_table soep3_table using "$tables/ob_soep_new.tex", refcat(temp "\textit{Individual covariates:}", nolabel) replace eqlabels(none) cells(ob_b(fmt(%9.4f) star pvalue(ob_p)) ob_se(fmt(%9.4f) par(( )))) keep(Summary:E Summary:C Summary:R E:jobtenure E:semp E:parttime E:uni E:male E:married E:immigrant E:age E:age_squared E:marginal E:temp) booktabs noobs width(\hsize) gaps coeflabel(Summary:E "Endowments" Summary:C "Coefficients" Summary:R "Raw difference" E:semp "Self-employed" E:parttime "Part-time worker" E:age "Age" E:age_squared "(Age/10)^{2}" E:male "Male" E:married "Married" E:union "Union member" E:immigrant "Immigrant" E:uni "Tertiary degree" E:marginal "Marginally employed" E:jobtenure "Job tenure" E:temp "Temporary worker") mlabels("1986-2004" "2004-2018" "1986-2018") nonumbers collabels("")  postfoot("\tabnotes{4}{This table displays the contribution of composition and coefficient differences to the overall difference in aggregate job insecurity between selected years, derived from the nonlinear Oaxaca-Blinder decomposition found in Powers et al. (2011). Standard errors in parentheses. *\textit{p} < 0.05, **\textit{p} < 0.01, ***\textit{p} < 0.001}") title("Oaxaca-Blinder decomposition results: Germany") order(Summary:R Summary:C Summary:E E:temp E:semp E:parttime E:marginal E:union E:jobtenure E:uni E:male E:Married E:immigrant E:age E:age_squared)
